﻿namespace Domains
{
    using System.Diagnostics;
    using System;
    using System.IO;
    using System.Threading;
    using HtmlAgilityPack;

    /// <summary>
    /// Static Class for scripts and programs execution
    /// </summary>
    public class Execution
    {
        /// <summary>
        /// Method for executing compare script
        /// </summary>
        /// <param name="path">Path of the application - parent folder</param>
        /// <param name="firstparam">First file for compare</param>
        /// <param name="secondparam">Second file for compare</param>
        /// <returns>ComparedFiles object</returns>
        public static void ExecCompareScript(string firstparam, string secondparam, string thirdparam)
        {
            CheckAndDelete(thirdparam);
            string comparepath = AppDomain.CurrentDomain.BaseDirectory + "Scripts\\CompareScript.txt";
            string param = " /silent @\"" + comparepath + "\"" + " " + "\"" + firstparam + "\"" + " " + "\"" + secondparam + "\"" + " " + "\"" + thirdparam + "\"";
            Process.Start(beyondpath, param);
        }

        /// <summary>
        /// Method for executing merge command
        /// </summary>
        /// <param name="leftfile">the name of left file</param>
        /// <param name="rightfile">the name of right file</param>
        /// <param name="ancestorfile">the name of ancestor file</param>
        /// <param name="mergefile">the name of merge file</param>
        public static void ExecMergeScript(string leftfile, string rightfile, string ancestorfile, string mergefile)
        {
            CheckAndDelete(mergefile);
            string mergepath = AppDomain.CurrentDomain.BaseDirectory + "Scripts\\MergeCommands.bat";
            string param = leftfile + " " + rightfile + " " + ancestorfile + " " + mergefile;
            ProcessStartInfo pr = new ProcessStartInfo(mergepath, param);
            pr.CreateNoWindow = true;
            pr.UseShellExecute = false;
            pr.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(pr);
        }

        /// <summary>
        /// Checking and deleting temporary files
        /// </summary>
        /// <param name="filename">the name of file to delete</param>
        public static void CheckAndDelete(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
        }

        /// <summary>
        /// Checking the possibility of file open and opening it after some time
        /// </summary>
        /// <param name="file">The name of the file</param>
        public static void ReadFile(string file)
        {
            StreamReader ReturnStreamReader = null;
            while (ReturnStreamReader == null)
            {
                try
                {
                    ReturnStreamReader = new StreamReader(file);
                }
                catch (Exception)
                {
                    Thread.Sleep(100);
                }
            }
            ReturnStreamReader.Close();
        }

        /// <summary>
        /// Converting HtmlDocument to string
        /// </summary>
        /// <param name="Doc">Object of HtmlDocument type</param>
        /// <returns>String</returns>
        public static string DocumentToString(HtmlDocument Doc)
        {
            string result = null;
            using (StringWriter writer = new StringWriter())
            {
                Doc.Save(writer);
                result = writer.ToString();
            }
            return result;
        }

        /// <summary>
        /// Writing string to file
        /// </summary>
        /// <param name="str">string</param>
        /// <param name="filename">file name</param>
        public static void StringToFile(string str, string filename)
        {
            StreamWriter NewStreamWriter = new StreamWriter(filename);
            NewStreamWriter.WriteLine(str);
            NewStreamWriter.Close();
        }

        /// <summary>
        /// Writing file to string
        /// </summary>
        /// <param name="filename">file name</param>
        /// <returns>string</returns>
        public static string FileToString(string filename)
        {
            ReadFile(filename);
            StreamReader FileReader = new StreamReader(filename);
            string ReaderString = default(string);
            while (!FileReader.EndOfStream)
            {
                ReaderString = ReaderString + FileReader.ReadLine();
            }
            return ReaderString;
        }

        /// <summary>
        /// The hole path to Beyond Compare Program
        /// </summary>
        public static string beyondpath;

    }
}
